package com.chen.pspms.controller;

import com.chen.pspms.entity.CompMajorStatistics;
import com.chen.pspms.entity.CompanyInfo;
import com.chen.pspms.entity.Majorhazardregistertable;
import com.chen.pspms.entity.Standardizationtable;
import com.chen.pspms.service.CompMajorStatisticsService;
import com.chen.pspms.service.CompanyService;
import com.chen.pspms.service.MajorhazardregisterService;
import com.chen.pspms.service.StandardizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/inspection")
public class InspectionController {
    @Autowired
    MajorhazardregisterService majorhazardregisterService;
    @Autowired
    CompanyService companyService;
    @Autowired
    CompMajorStatisticsService compMajorStatisticsService;
    @Autowired
    StandardizationService standardizationService;

    @RequestMapping("/list")
    public String lnslist(Model model) {
        //显示巡检工作人员首页信息
        //查询所有企业信息
        List<CompanyInfo> companylist = companyService.findAll();
        //根据企业信息，查询企业危险源统计信息
        List<CompMajorStatistics> compMajorStatisticsList = compMajorStatisticsService.getListByCompany(companylist);
        //存储信息
        model.addAttribute("compMajorStatisticsList",compMajorStatisticsList);
        //处理业务返回结果
        return "重大危险源管理/巡检危险源管理页面";
    }

    @RequestMapping("/show")
    public String show(Integer id, Model model){
        //调用业务处理
        if(id==0) {
            //如果是新增，直接返回新增页面
            return "重大危险源管理/巡检新建";
        }else {
            //如果是修改，查询数据后返回
            Majorhazardregistertable majorhazardregistertable = majorhazardregisterService.findyById(id);
            model.addAttribute("majorhazardregistertable", majorhazardregistertable);
        }
        //处理业务返回结果
        return "重大危险源管理/巡检修改";
    }

    @RequestMapping("/save")
    public String save(Majorhazardregistertable majorhazardregistertable, Model model) {
        //调用业务处理
        String exist =null;
        if(companyService.findyByName(majorhazardregistertable.getCompany().getName())==null) {
            //如果员工合同表已存在
            exist="该企业不存在";
        }else {
            //企业存在，则添加
            //查询企业和查询生产标准化信息
            CompanyInfo company = companyService.findyByName(majorhazardregistertable.getCompany().getName());
            Standardizationtable standardizationtable =        //查找并设置企业，生产标准
                    standardizationService.findyByType(majorhazardregistertable.getStandardizationtable().getHazardtype());
            //设置企业和生产标准化信息
            majorhazardregistertable.setCompany(company);
            majorhazardregistertable.setStandardizationtable(standardizationtable);
            //判定等级并设置
            String level = standardizationService.getLevel(majorhazardregistertable.getNumber(),standardizationtable);
            majorhazardregistertable.setLevel(level);
//            System.out.println(majorhazardregistertable);
            majorhazardregisterService.save(majorhazardregistertable);
        }
        //处理业务返回结果
        //查询所有企业信息
        List<CompanyInfo> companylist = companyService.findAll();
        //根据企业信息，查询企业危险源统计信息
        List<CompMajorStatistics> compMajorStatisticsList = compMajorStatisticsService.getListByCompany(companylist);
        //存储信息
        model.addAttribute("compMajorStatisticsList",compMajorStatisticsList);
        model.addAttribute("exist", exist);
        return "重大危险源管理/巡检危险源管理页面";
    }

    @RequestMapping("/glance")
    public String glance(Integer id, Model model){
        //根据公司id，返回危险源信息，不返回无的
        //查询公司
        CompanyInfo company = companyService.findyById(id);
        //查询企业危险源信息列表
        List<Majorhazardregistertable> allMajorhazardregisterList=majorhazardregisterService.findyListByComId(id);
        //去除正常的
        List<Majorhazardregistertable> majorhazardregisterList=majorhazardregisterService.removeSafe(allMajorhazardregisterList);
//        System.out.println(majorhazardregisterList);
        model.addAttribute("company",company);
        model.addAttribute("majorhazardregisterList",majorhazardregisterList);
        return "重大危险源管理/巡检浏览";
    }

    @RequestMapping("/delete")
    public String delete(Integer id) {
        majorhazardregisterService.delete(id);
        return "redirect:/inspection/list";
    }

    @RequestMapping("/update")
    public String update(Majorhazardregistertable majorhazardregistertable, Model model) {
        //调用业务处理
        Standardizationtable standardizationtable =
                standardizationService.findyById(majorhazardregistertable.getStandardizationtable().getId());
        String level = standardizationService.getLevel(majorhazardregistertable.getNumber(),standardizationtable);
        majorhazardregistertable.setLevel(level);
//        System.out.println(majorhazardregistertable);
        majorhazardregisterService.update(majorhazardregistertable);
        //处理业务返回结果
        return "redirect:/inspection/list";//重定向就是在字符串前面加上前�? redirect:
    }

}
